﻿Imports System.Data.OleDb

Public Class ArticleDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            If Request.Params("AID") = "" Then Response.Redirect("Default.aspx")
            Dim oleDbConn2 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString2 As String = "select * from tblComments where Article_FKID = @f1"
            Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn2)
            cmd1.CommandType = CommandType.Text
            cmd1.Parameters.AddWithValue("@f1", Request.Params("AID"))
            oleDbConn2.Open()
            Dim dataReader = cmd1.ExecuteReader
            grdComments.DataSource = dataReader
            grdComments.DataBind()


            Dim ProgressViewSQL As String = "insert into tblArticleViews (wlUserName, Article_FKID) values (@f1, @f2)"
            Dim cmdViews As OleDbCommand = New OleDbCommand(ProgressViewSQL, oleDbConn2)
            cmdViews.CommandType = CommandType.Text
            If User.Identity.IsAuthenticated Then
                cmdViews.Parameters.AddWithValue("@f1", User.Identity.Name)
            Else
                cmdViews.Parameters.AddWithValue("@f1", "Anonymous")
            End If

            cmdViews.Parameters.AddWithValue("@f1", Request.Params("AID"))
            cmdViews.ExecuteNonQuery()

        End If


        If User.Identity.IsAuthenticated Then
            phCommentBox.Visible = True
            phLogintoComment.Visible = False
        Else
            phCommentBox.Visible = False
            phLogintoComment.Visible = True
        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into tblComments(Addedby,Title,Details,DateAdded,Article_FKID) Values (@f1,@f2,@f3,@f4,@f5)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", txtACTitle.Text)
        cmd.Parameters.AddWithValue("@f3", txtAComment.Text)
        cmd.Parameters.AddWithValue("@f4", Date.Today)
        cmd.Parameters.AddWithValue("@f5", Request.Params("AID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Fill(grdComments)

        Dim fillComments As String = "select * from tblComments where Article_FKID = @f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("AID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader
        grdComments.DataSource = dataReader
        grdComments.DataBind()


        txtACTitle.Text = ""
        txtAComment.Text = ""
    End Sub
End Class